﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:商品规格
    /// </summary>
    public partial class article_spec
    {
        public SqlSugarClient db;
        private string databaseprefix;//数据库表名前缀
        public article_spec(string _databaseprefix)
        {
            db = new DBUtility.DbContext().Db;
            databaseprefix = _databaseprefix;
        }


        #region 基本方法========================


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_article_spec model)
        {
            try
            {
                db.BeginTran();
                model.id = db.Insertable(model).ExecuteReturnIdentity();
                if (model.values != null)
                {
                    foreach (Entitys.article_spec_value modelt in model.values)
                    {
                        db.Insertable(modelt).AS(databaseprefix + "article_spec").ExecuteCommand();
                    }
                }
                db.CommitTran();
            }
            catch
            {
                db.RollbackTran();
                return 0;
            }
            return model.id;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_article_spec model)
        {
            try
            {
                db.BeginTran();
                model.id = db.Updateable(model).ExecuteCommand();
                //删除已删除的规格值
                DeleteList(model.values, model.id);
                //添加/修改规格值
                if (model.values != null)
                {
                    foreach (Entitys.article_spec_value modelt in model.values)
                    {
                        if (modelt.id > 0)
                        {
                            db.Updateable<Entitys.dt_article_spec>(modelt).AS(databaseprefix + "article_spec").ExecuteCommand();
                        }
                        else
                        {
                            db.Insertable(modelt).AS(databaseprefix + "article_spec").ExecuteCommand();
                        }
                    }
                }

                db.CommitTran();
            }
            catch(Exception ex)
            {
                db.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            try
            {
                db.BeginTran();
                db.Deleteable<Entitys.dt_article_spec>().Where(x => x.parent_id == id).ExecuteCommand();
                db.Deleteable<Entitys.dt_article_spec>().Where(x => x.id == id).ExecuteCommand();
             

                db.CommitTran();
            }
            catch
            {
                db.RollbackTran();
                return false;
            }
            return true;
        }

        public Entitys.dt_article_spec GetModel(int id)
        {
            Entitys.dt_article_spec model = db.Queryable<Entitys.dt_article_spec>().Where(x => x.id == id).First();
            model.values = db.Queryable<Entitys.article_spec_value>().AS(databaseprefix + "article_spec").Where(x => x.parent_id == model.id).ToList();
            return model;
        }

        #endregion

        /// <summary>
        /// 查找不存在的规格值并删除
        /// </summary>
        private void DeleteList(List<Entitys.article_spec_value> models, int parentId)
        {
            if (models != null)
            {
                List<int> idList = new List<int>();
                foreach (Entitys.article_spec_value modelt in models)
                {
                    if (modelt.id > 0)
                    {
                        idList.Add(modelt.id);
                    }
                }
                if (idList.Count>0)
                {
                    db.Deleteable<Entitys.dt_article_spec>().Where(x => x.parent_id == parentId && !idList.Contains(x.id));
                }
            }
        }



        /// <summary>
        /// 返回栏目规格列表
        /// </summary>
        public List<Entitys.dt_article_spec> GetCategorySpecList(int category_id)
        {
            return db.Queryable<Entitys.dt_article_spec, Entitys.dt_article_category_spec>((s, c) => new JoinQueryInfos(
                 JoinType.Inner, s.id == c.spec_id
                 ))
                .Where((s, c) => c.category_id == category_id)
                .OrderBy((s, c) => s.sort_id, OrderByType.Asc)
                .OrderBy((s, c) => s.id, OrderByType.Desc)
                .ToList();

        }
    }
}